package org.theSeed.controller;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.theSeed.base.pojo.dto.CommonKeyValue;
import org.theSeed.base.pojo.vo.R;
import org.theSeed.pojo.po.SysResource;
import org.theSeed.pojo.ro.EditResourceRo;
import org.theSeed.service.SysResourceService;

import javax.validation.Valid;
import java.util.List;

/**
 * 资源管理
 */
@RestController
@RequestMapping("sysResource")
public class SysResourceController {
    @Autowired
    private SysResourceService sysResourceService;

    /**
     * 菜单列表
     * @return
     */
    @GetMapping( "listResource")
    public R<List<SysResource>> listResource(){
        return R.success(sysResourceService.listResource());
    }

    /**
     * 菜单详情
     * @param id
     * @return
     */
    @GetMapping( "getResourceDetail")
    public R<SysResource> getResourceDetail(@PathVariable String id){
        return R.success(sysResourceService.getResourceDetail(id));
    }

    /**
     * 编辑菜单
     * @param editResourceRo
     * @return
     */
    @PostMapping( "editResource")
    public R editResource(@RequestBody @Valid EditResourceRo editResourceRo){
        if (ObjectUtil.isNotNull(editResourceRo.getId())){
            editResourceRo.setResourceCode(null);
        }
        sysResourceService.editResource(editResourceRo);
        return R.success();
    }

    /**
     * 启用停用角色
     * @return
     */
    @PostMapping( "enableResource")
    public R enableResource(@RequestBody @Valid CommonKeyValue<String,String> enableRo){
        sysResourceService.enableResource(enableRo);
        return R.success();
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @DeleteMapping( "deleteResource/{id}")
    public R deleteResource(@PathVariable String id){
        sysResourceService.deleteResource(id);
        return R.success();
    }
}
